import type { PlatformHideConfig } from '../../../lib/abstract/base_hide_module'
import type { ComponentStyleIdType } from '../../../types/interfaces'
import { BaseHideModule } from '../../../lib/abstract/base_hide_module'
import {
  BING_SEARCH_PAGE_SELECTORS,
  BingPageType,
} from '../../../lib/page_handler/platform/bing/types'
import {
  SO_SEARCH_PAGE_SELECTORS,
  SoPageType,
} from '../../../lib/page_handler/platform/so/types'
import {
  SOGOU_SEARCH_PAGE_SELECTORS,
  SogouPageType,
} from '../../../lib/page_handler/platform/sogou/types'
import { ComponentStyleId, logger, SUPPORTED_PLATFORMS } from '../../../types/constants'
import { ConfigManager } from '../../config_manager'
import { SearchDropdownType } from './style_enhancement/platforms'

interface Config {
  enabled?: boolean
}

export class HideSearchHotLists extends BaseHideModule {
  readonly id: string = 'hide_search_hot_lists'
  readonly styleId: string = ComponentStyleId[this.name]
  isInitialized: boolean = false
  readonly supportedPlatforms: string[] = ConfigManager.getInstance().moduleSupportedPlatforms(this.name)
  private static instance: HideSearchHotLists

  config: Config = {
    enabled: true,
  }

  get name(): ComponentStyleIdType {
    return 'HideSearchHotLists'
  }

  /**
   * 获取单例实例
   */
  public static getInstance(): HideSearchHotLists {
    if (!HideSearchHotLists.instance) {
      HideSearchHotLists.instance = new HideSearchHotLists()
    }
    return HideSearchHotLists.instance
  }

  /**
   * 清理操作
   */
  handleCleanupOthers(): void {
    this.applyHide(true, SearchDropdownType.HotRank)
  }

  core(): void {
    this.loadConfig()

    if (!this.config.enabled)
      return

    this.applyHide(false, SearchDropdownType.HotRank)
  }

  onToggleCallback(_id: string, checked: boolean): void {
    super.onToggleCallback(_id, checked)
  }

  getPlatformConfig(): PlatformHideConfig {
    return {
      [SUPPORTED_PLATFORMS.BING]: this.createPlatformConfig(
        BING_SEARCH_PAGE_SELECTORS,
        [
          BingPageType.Web,
          BingPageType.Video,
          BingPageType.Index,
        ],
        SearchDropdownType.HotRank,
      ),
      [SUPPORTED_PLATFORMS.SO_GOU]: this.createPlatformConfig(
        SOGOU_SEARCH_PAGE_SELECTORS,
        [
          SogouPageType.Web,
          SogouPageType.Video,
        ],
        SearchDropdownType.HotRank,
      ),
      [SUPPORTED_PLATFORMS.SO]: this.createPlatformConfig(
        SO_SEARCH_PAGE_SELECTORS,
        [
          SoPageType.News,
        ],
        SearchDropdownType.HotRank,
      ),
    }
  }

  applyConfig(config: Partial<Config>): void {
    super.updateConfig(config)

    this.applyHide(false, SearchDropdownType.HotRank)

    logger.info(`[${this.name}] Config updated and apply`, this.config)
  }

  getStyles(): string {
    return ``
  }
}
